#!/usr/bin/env bash
#
# VMware Installer Service command line interface.
#

set -e

trap "" USR1 # Used for canceling installation, ignore.

ETCDIR=/etc/vmware-installer

MIN_GLIBC_VERSION=2.5


# Check to make sure glibc version >= MIN_GLIBC_VERSION, meet the minimum
# glibc version requirement to launch vmis-launcher. Function returns with
# no side effect if version check succeeds, exits with 1 if version check
# fails, but only emits a warning if the version cannot be determined.
validate_glibc_version() {
   # Example ldd output:
   #   ubuntu:
   #      ldd (Ubuntu EGLIBC 2.12.1-0ubuntu10.2) 2.12.1
   #      ...
   #   other linux distributions:
   #      ldd (GNU libc) 2.12
   #      ...
   read glibcMajor glibcMinor <<<`ldd --version | awk '/^ldd/ { split($NF,v,/\./); print v[1],v[2]; }'`
   if [ "$glibcMinor" == "" ]; then
      echo "Warning: VMware Installer could not determine this system's glibc version."
      return
   fi

   # Make sure glibc version >= MIN_GLIBC_VERSION
   minVersion=$(expr `echo $MIN_GLIBC_VERSION | awk -F. '{print $1*65536+$2}'`)
   glibcVersion=$(expr $glibcMajor \* 65536 + $glibcMinor)

   if [ $glibcVersion -lt $minVersion ]; then
      echo "Error: This system's glibc (version $glibcMajor.$glibcMinor) is too old. This product requires glibc version $MIN_GLIBC_VERSION or later."
      exit 1
   fi
}

# VMWARE_BOOTSTRAP is overriden in the bootstrapper so that it can be
# sourced from a temporary location during installation.
if [ -e "$VMWARE_BOOTSTRAP" ]; then
   . "$VMWARE_BOOTSTRAP"
else
   . $ETCDIR/bootstrap
fi

. "$VMWARE_INSTALLER"/python/init.sh

# vmis-launcher requires glibc versioin >= MIN_GLIBC_VERSION
validate_glibc_version

VMWARE_INSTALLER="$VMWARE_INSTALLER" VMISPYVERSION="$VMISPYVERSION" "$VMWARE_INSTALLER"/vmis-launcher "$VMWARE_INSTALLER"/vmware-installer.py "$@"
